package edu.nju.desserthouse.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import nju.edu.desserthouse.model.Vip;

import edu.nju.desserthouse.dao.VipDao;

/**
 * Session Bean implementation class VipDao
 */
@Stateless
public class VipDaoBean implements VipDao{

	private static final long serialVersionUID = 1L;
	
	@PersistenceContext(unitName = "desserthouseJPA")
	protected EntityManager em;
	
	public VipDaoBean(){
		
	}
	
	@Override
	public boolean save(Vip vip) {
		try{
			em.persist(vip);
		}catch(Exception e){
			e.printStackTrace();
			 return false;
		}
		return true;
	}

	@Override
	public Vip find(String column, String value) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v." + column + "='" + value + "'");
			vip = (Vip)query.getSingleResult();
			em.clear();
			return vip;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean updateByVipId(Vip vip) {
		try{
			em.merge(vip);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public Vip find(String col1, String val1, String col2, String val2) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v." + col1 + "= '" + val1 + "' and v." + col2 + "= '" + val2 + "'");
			vip = (Vip) query.getSingleResult();
			em.clear();
			return vip;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean delete(String col1, String val1, String col2, String val2) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v." + col1 + "= '" + val1 + "' and v." + col2 + "= '" + val2 + "'");
			vip = (Vip) query.getSingleResult();
			if(vip != null){
				vip.setState(2);
				save(vip);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean payfee(String vip_id) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v.id= '" + vip_id + "'");
			vip = (Vip) query.getSingleResult();
			if(vip != null){
				vip.setState(0);
				Date now = new Date(new java.util.Date().getTime());
				vip.setLastpay(now);
				save(vip);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean charge(String vip_id, int fee) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v.id= '" + vip_id + "'");
			vip = (Vip) query.getSingleResult();
			if(vip != null){
				double balance = vip.getBalance();
				balance += fee;
				vip.setBalance(balance);
				save(vip);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean activate(String vip_id) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v.id= '" + vip_id + "'");
			vip = (Vip) query.getSingleResult();
			if(vip != null){
				double balance = vip.getBalance();
				balance += 100;
				vip.setBalance(balance);
				vip.setState(0);
				Date now = new Date(new java.util.Date().getTime());
				vip.setLastpay(now);
				save(vip);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean buy(String vip_id, double price) {
		Vip vip = null;
		try{
			Query query = em.createQuery("from Vip v where v.id= '" + vip_id + "'");
			vip = (Vip) query.getSingleResult();
			if(vip != null){
				double balance = vip.getBalance();
				balance -= price;
				vip.setBalance(balance);
				
				double total = vip.getTotalconsume();
				total += price;
				vip.setTotalconsume(total);
				
				int level = (int) (total/200);
				if(level >= 9){
					level = 9;
				}
				vip.setLevel(level);
				
				updateByVipId(vip);
				return true;
			}else{
				System.out.println("NULL!!!");
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public ArrayList<Vip> getVipList() {
		try{
			Query query = em.createQuery("from Vip v where v.id > -1");
			@SuppressWarnings("unchecked")
			List<Vip> vip = query.getResultList();
			if(vip != null){
				ArrayList<Vip> vipList = new ArrayList<Vip>(vip);
				em.clear();
				return vipList;
			}else{
				System.out.println("NULL RESULT!!!");
				em.clear();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public ArrayList<Vip> getVipList(Date date) {
		try{
			Query query = em.createQuery("from Vip v where v.lastpay = '" + date +"'");
			@SuppressWarnings("unchecked")
			List<Vip> vip = query.getResultList();
			if(vip != null){
				ArrayList<Vip> vipList = new ArrayList<Vip>(vip);
				em.clear();
				return vipList;
			}else{
				System.out.println("NULL RESULT!!!");
				em.clear();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public ArrayList<String> getAddressList() {
		ArrayList<String> addrList = new ArrayList<String>();
		try{
			Query query = em.createQuery("from Vip v where v.id > -1");
			@SuppressWarnings("unchecked")
			List<Vip> vip = query.getResultList();
			if(vip != null){
				ArrayList<Vip> vipList = new ArrayList<Vip>(vip);
				em.clear();
				boolean contain = false;
				for(int i = 0; i < vipList.size(); i++){
					if(addrList.size() == 0){
						addrList.add(vipList.get(i).getAddr());
					}else{
						for(int j = 0; j < addrList.size(); j++){
							contain |= ((vipList.get(i).getAddr()).equals(addrList.get(j)));
						}
						if(contain == false){
							addrList.add(vipList.get(i).getAddr());
						}else{
							contain = false;
						}
					}
				}
				return addrList;
			}else{
				System.out.println("NULL RESULT!!!");
				em.clear();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
